﻿using System;
using System.Collections.Generic;
using System.Text;

namespace osuStd2Key
{
    public abstract class IConverter
    {
        public delegate void LogD(string tag, string msg);
        public delegate void LogExcD(Exception e, string msg = null);
        public delegate void ConvertResultCallback(bool success, string msg);
        private LogD _LogDelegater;
        private LogExcD _LogExcDelegater;
        private ConvertResultCallback _ConvertResultCallback;
        private bool _Success = false;
        private string _Msg = "";
        public abstract void DoConvert();
        public void Convert()
        {
            _Success = true;
            try
            {
                DoConvert();
            }
            catch (Exception e)
            {
                if (_LogExcDelegater != null)
                    _LogExcDelegater(e);
            }
            
            if (_ConvertResultCallback != null)
                _ConvertResultCallback(_Success, _Msg);
        }
        protected void SetConvertResult(bool success, string msg = null)
        {
            _Msg = msg;
            _Success = success;
        }
        public void SetLogDelegate(LogD d)
        {
            _LogDelegater = d;
        }
        public void SetLogException(LogExcD d)
        {
            _LogExcDelegater = d;
        }
        public void SetConvertResultCallback(ConvertResultCallback d)
        {
            _ConvertResultCallback = d;
        }
        protected void Log(string tag, string msg)
        {
            if (_LogDelegater != null)
                _LogDelegater(tag, msg);
        }
        protected void LogExc(Exception e, string msg = null)
        {
            if (_LogExcDelegater != null)
                _LogExcDelegater(e, msg);
        }
    }
}
